<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>上岗测试</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="apple-mobile-web-app-capable" content="yes">

		<link rel="stylesheet" href="../../css/mui.min.css">
		<link rel="stylesheet" href="../../css/mui-ext.css">

		<style type="text/css">
			
			
		</style>
	</head>

	<body>
		<div class="content" style="margin-bottom: 100px;">
			<div id="dv-test-content">
				<ul style="list-style: none;" id="dv-test-list">
					<!--
                    	作者：xuzhao_deng@sina.com
                    	时间：2016-12-17
                    	描述：动态试题
                    -->
				</ul>
			</div>
			<input type="hidden" id="testNum" value=""/>
			<nav class="mui-bar mui-bar-tab" style="height: 40px;">
				<div style="float: right; margin-right: 10px; margin-bottom: 5px; margin-top: 5px;">
					<button onclick="fun_test_submit();" style="width: 70px;">提交</button>
				</div>
			</nav>
		</div>
	</body>
	<!-- APP JS -->
	<script src="../../js/mui.min.js"></script>
	<script src="../../js/utils/http.utils.js"></script>
	<script src="../../js/bus/pubfun.js"></script>
	<script>
	
		var $test_id = getQueryString(window.location.search, "testid");
		var $userLogin = JSON.parse(localStorage.getItem("login-user-parent"));
	
		mui.init({
		  preloadPages:[{
		    id:"test345.html",
		    url:"test345.html"           
		  }]
		});
	
		function fun_structure_test_template(num, testInfo){
			var $testDivTem;
			var $choiceLst = testInfo.questionContent.choiceList;
			if(testInfo.type == 1 || testInfo.type == 2) {//单选题
				var $inputType;
				if(testInfo.type == 1){
					$inputType = "radio";
				}
				if(testInfo.type == 2){
					$inputType = "checkbox";
				}
				$testDivTem =
				'<li><div id="dv-test-title" style="background-color: #F1F1F1;">'
				+	'<div id="question-icon" style="margin-left: -30px;">'
				+		'<span><img src="../../img/posts/question.png"/></span>'
				+	'</div>'
				+	'<div id="question-title" style="margin: -50px 0px 0px 30px; font-size: 18px;">'
				+		'<span style="line-height: 130%; color: #428bca;">'+ testInfo.questionContent.title +'</span>'
				+	'</div>'
				+'</div>'
				+'<div id="dv-test-option" style="margin-left: -50px; margin-top: 23px;">'
				+	'<ul style="list-style: none; line-height: 30px;">'
				+		'<li><input type="'+ $inputType +'" value="A" name="question-radio'+ num +'">&nbsp;A: '+ $choiceLst.A +'</span>'
				+		'</li>'
				+		'<li><input type="'+ $inputType +'" value="B" name="question-radio'+ num +'">&nbsp;B: '+ $choiceLst.B +'</span>'
				+		'</li>'
				+		'<li><input type="'+ $inputType +'" value="C" name="question-radio'+ num +'">&nbsp;C: '+ $choiceLst.C +'</span>'
				+		'</li>'
				+		'<li><input type="'+ $inputType +'" value="D" name="question-radio'+ num +'">&nbsp;D: '+ $choiceLst.D +'</span>'
				+		'</li>'
				+	'</ul>'
				+'</div><br /></li>';
			}
			
			if(testInfo.type == 3){ //判断题
				$testDivTem =
				'<li>'
				+	'<div id="dv-test-title" style="background-color: #F1F1F1;">'
				+		'<div id="question-icon" style="margin-left: -30px;">'
				+			'<span><img src="../../img/posts/question.png"/></span>'
				+		'</div>'
				+		'<div id="question-title" style="margin: -50px 0px 0px 30px; font-size: 18px;">'
				+			'<span style="line-height: 130%; color: #428bca;">'+ testInfo.questionContent.title +'</span>'
				+		'</div>'
				+	'</div>'
				+	'<div id="dv-test-option" style="margin-left: -50px; margin-top: 25px;">'
				+		'<ul style="list-style: none; line-height: 30px; display: flex;">'
				+			'<li><input type="radio" value="T" name="question-radio'+ num +'">&nbsp;是</span></li>'
				+			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
				+			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
				+			'<li><input type="radio" value="F" name="question-radio'+ num +'">&nbsp;否</span></li>'
				+		'</ul>'
				+	'</div><br />'
				+'</li>';
			}
			
			return $testDivTem;
		}
	
		
		window.onload = function(){
			
			if($test_id == 11){
				document.title = "345调查";
			}
			if($test_id == 12){
				document.title = "3有评测";
			}
			if($test_id == 13){
				document.title = "4要评测";
			}
			if($test_id == 14){
				document.title = "5会评测";
			}
			mui.ajax({
				url : $request_url.testPaper,
				type : "get",
				dataType : "json",
				headers: {
					"Authorization": $headers_authorization
				},
				data : {
					testPaperId : $test_id
				},
				success : function(dat){
					if(dat.code == 0){
						var $divTestLst = document.getElementById("dv-test-list");
						var testLst = dat.result.questions;
						for(var i = 0; i < testLst.length; i ++){
							var $li = document.createElement('li');
							$li.innerHTML = fun_structure_test_template(i, testLst[i]);
							$divTestLst.appendChild($li);
						}
						document.getElementById("testNum").value = testLst.length;
					} else {
						mui.toast(dat.message);
						return;
					}
				}, error : function(err){
					mui.toast("服务繁忙...");
					return;
				}
			});
			
		}
		
		function fun_checkAnswer($ans){
			var $nullCount = 0;
			var $ansArr = $ans.split(",");
			for (var i = 0; i < $ansArr.length; i ++) {
				if($ansArr[i] == null || $ansArr[i] == "" || $ansArr[i] == undefined){
					$nullCount ++;
				}
			}
			return $nullCount;
		}
		
		
		function fun_test_submit(){
			console.clear();
			var $iptfix = "question-radio";
			var $testConnt = document.getElementById("testNum").value; //获取题总数
			var $answer = "";
			for(var i = 0; i < $testConnt; i ++){
				var $question_answer = document.getElementsByName($iptfix + i); 
				var $question_desc = "";
				for(var j = 0; j < $question_answer.length; j ++){
					if($question_answer[j].checked){
						$question_desc += $question_answer[j].value + ".";
					}
				}
				$answer += $question_desc.substr(0, $question_desc.length - 1) + ",";
				console.log(($iptfix + i) + " = " + $question_desc);
			}
			$answer = $answer.substr(0, $answer.length - 1);
			
			$aswNum = fun_checkAnswer($answer);
			if($aswNum > 0){
				mui.alert("您还有" + $aswNum + "道题还没有答");
				return;
			} else {
				mui.ajax({
					url : $request_url.saveAnswer,
					type : "post",
					dataType : "json",
					headers: {
						"Authorization": $headers_authorization
					},
					data : {
						userId : $userLogin.userId,
						testPaperId : $test_id,
						answers : $answer
					},
					timeout : 10000,
					success : function(dat){
						if(dat.code == 0){
							console.log(dat.result);
						} else {
							mui.toast(dat.message);
							return;
						}
					}, error : function(err){
						mui.toast("提交失败，超等片刻在提交吧");
						return;
					}
				});
			}
			console.log($answer.split(",").length);
		}
	</script>

</html>